package com.it.gw.modules.station.activity

import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.it.common.utils.DisplayUtil
import com.it.common.utils.RvSpacesItemUtils
import com.it.data.network.rsp.WtGQVo
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityGqstationRecordBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.station.adapter.GQStationRecordAdapter


//运维记录列表
class GQStationRecordActivity : BaseActivity<ActivityGqstationRecordBinding>() {


    companion object {
        const val TAG = "OperationRecordActivity"
        const val PAGE_SIZE = 20
        const val TYPE_QUESTION = "type_question"
        const val TYPE_CHECK = "TYPE_CHECK"
        const val TYPE = "type"
    }

    var type = 1
    private var pageNum = 1
    private val list by lazy { mutableListOf<WtGQVo>() }
    private val adapter by lazy {
        GQStationRecordAdapter(context = this, datas = list, listener = {
            val intent = Intent(this, GQStationReportDetailActivity::class.java)
            intent.putExtra("id", it.id)
            intent.putExtra("stcd", it.stcd)
            intent.putExtra("queDeal", it.queDeal)
            intent.putExtra("type", type)
            intent.putExtra("orderId", intent.getStringExtra("orderId"))

            startActivity(intent)
        })
    }


    override fun initObserver() {
        gqStationViewModel.pageWtGQVoListLiveData.observe(this) {
            ProgressDialog.hide()
            loadMoreData = false
            binding.swipeRefreshLayout.isRefreshing = false
            if(it.result){
                if (it.data?.list.isNullOrEmpty()) {
                    if (pageNum > 1) {
                        pageNum--
                        Toast.makeText(this, "没有更多数据了！", Toast.LENGTH_SHORT).show()
                    } else {
                        //第一次就没数据
                        list.clear()
                        adapter.notifyDataSetChanged()
//                    llNoData.setVisibility(View.VISIBLE);
//                    srHasData.setVisibility(View.GONE);
                    }

                } else {
//                llNoData.setVisibility(View.GONE);
//                srHasData.setVisibility(View.VISIBLE);
                    if (pageNum == 1) {
                        list.clear()
                    }
                    list.addAll(it.data!!.list!!)
                    adapter.notifyDataSetChanged()

                }
            }
            else{
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_LONG).show()
            }

        }
    }


    override fun initListener() {
        //请选择行政区划
        binding.addvcd.setOnClickListener {
            showAddvcdWheelPickDialog(okAction = {
                val text2 =
                    if (select2Position >= 0) list2?.get(select2Position)?.text else ""
                val text3 =
                    if (select3Position >= 0) list3?.get(select3Position)?.text else ""



                if (!TextUtils.isEmpty(text2) && !TextUtils.isEmpty(text3)) {
                    binding.addvcd.text = "${text2}/${text3}"
                    addvcd = list3?.get(select3Position)?.id
                } else if (!TextUtils.isEmpty(text2)) {
                    binding.addvcd.text = "${text2}"
                    addvcd = list2?.get(select2Position)?.id
                } else if (!TextUtils.isEmpty(text3)) {
                    binding.addvcd.text = "${text3}"
                    addvcd = list3?.get(select3Position)?.id
                } else {
                    binding.addvcd.text = null
                    addvcd = null
                }
            })
        }
        //请选择运维状态
        binding.quedeal.setOnClickListener {
            showQuedealWheelViewDialog(okAction = { index ->
                binding.quedeal.text = queDealList?.get(index)?.second
                quedeal = queDealList?.get(index)?.first
            })
        }
//请选择运维状态
        binding.queType.setOnClickListener {
            showQuaTypeWheelViewDialog(okAction = { index ->
                binding.queType.text = quaTypeList?.get(index)?.data_Name
                quaType = quaTypeList?.get(index)?.id
                Log.d(TAG, "initListener: ")
            })
        }

        quaTypeList

        binding.rlSearch.setOnClickListener {
            ProgressDialog.show(this)
            pageNum = 1
            getWtGQ()
        }

        binding.rlQuestion.setOnClickListener {
            binding.tvQuetsion.setTextColor(ContextCompat.getColor(this, R.color.color_0F6AF8))
            binding.vLine1.visibility = View.VISIBLE
            binding.tvCheck.setTextColor(ContextCompat.getColor(this, R.color.color_999999))
            binding.vLine2.visibility = View.INVISIBLE
            list.clear()
            adapter.notifyDataSetChanged()
            pageNum = 1
            type = 1
            visit = false
            showSearchView()
            getWtGQ()
        }
        binding.rlCheck.setOnClickListener {
            binding.tvCheck.setTextColor(ContextCompat.getColor(this, R.color.color_0F6AF8))
            binding.vLine2.visibility = View.VISIBLE
            binding.tvQuetsion.setTextColor(ContextCompat.getColor(this, R.color.color_999999))
            binding.vLine1.visibility = View.INVISIBLE
            list.clear()
            adapter.notifyDataSetChanged()
            pageNum = 1
            type = 2
            visit = false
            showSearchView()
            getWtGQ()
        }
        binding.swipeRefreshLayout.setOnRefreshListener {
            pageNum = 1
            getWtGQ()
        }
        binding.rvList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                // 获取布局管理器
                val layoutManager = recyclerView.layoutManager as LinearLayoutManager

                // 获取最后一个可见的 item 的位置
                val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()

                // 获取总的 item 数量
                val totalItemCount = layoutManager.itemCount

                // 判断是否滑动到了底部
                if (totalItemCount >= PAGE_SIZE && lastVisibleItemPosition == totalItemCount - 1) {
                    // 滑动到了底部，执行相关操作
                    loadMoreData()
                }
            }
        })


    }
    private var loadMoreData = false;
    private fun loadMoreData() {
        if(loadMoreData){
            return
        }
        Log.d(TAG, "loadMoreData: ")
        pageNum += 1
        loadMoreData = true;
        getWtGQ()
    }

    override fun initData() {
        ProgressDialog.show(this)
        var stcd = intent.getStringExtra("stcd")
        binding.stnm.setText(stcd)
        getWtGQ()
    }

    private fun getWtGQ() {
        gqStationViewModel.getWtGQ(
            pageNum = pageNum,
            pageSize = PAGE_SIZE,
            stcd = binding.stnm.text.toString(),
            type = type,
            trcd = addvcd,
            queType = quaType,
            queDeal = quedeal,
        )
    }

    var visit = false;
    override fun initView() {
        setTitle(
            title = R.string.str_operation_record,
            isBack = true,
            tvRightRes1 = R.string.str_select,
            tvRight1Action = {
                visit = !visit
                showSearchView()
            }
        )
        val map = hashMapOf(RvSpacesItemUtils.BOTTOM_DECORATION to DisplayUtil.dip2px(this, 10f))
        binding.rvList.addItemDecoration(RvSpacesItemUtils(map))
        binding.rvList.adapter = adapter

    }

    private fun showSearchView() {
        var stcd = intent.getStringExtra("stcd")
        binding.stnm.setText(stcd)
        addvcd = ""
        quedeal = ""
        quaType = ""
        binding.addvcd.text = addvcd
        binding.quedeal.text = quedeal
        binding.queType.text = ""
        if (visit) {
            binding.llSearch.visibility = View.VISIBLE

        } else {
            binding.llSearch.visibility = View.GONE
        }
    }


    override fun setContentView() {
        binding = ActivityGqstationRecordBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }


}